﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Prism.Dialogs;
using 算法入门.Domain.Operations;

namespace 算法入门.ViewModels
{
    public class BitPermissionViewModel: BindableBase
    {
        private readonly IDialogService _dialogService;

        #region 依赖属性
        /// <summary>
        /// 角色列表
        /// </summary>
        private ObservableCollection<User> userList;

        public ObservableCollection<User> UserList
        {
            get { return userList; }
            set { userList = value; RaisePropertyChanged(); }
        }

        #endregion

        #region 命令

        /// <summary>
        /// 分配权限
        /// </summary>
        public DelegateCommand<User> SetPerCommand { get; private set; }

        /// <summary>
        /// 删除权限
        /// </summary>
        public DelegateCommand<User> DelPerCommand { get; private set; }

        #endregion


        public BitPermissionViewModel(IDialogService dialogService)
        {
            _dialogService = dialogService;
            InitUserData();

            SetPerCommand = new DelegateCommand<User>(SetPerCommandFunc);
            DelPerCommand = new DelegateCommand<User>(DelPerCommandFunc);
        }

        #region 删除权限
        private void DelPerCommandFunc(User user)
        {
            string info = $"删除权限:{user.UserName}";
            OpenDialogForSetPer(user, "DelPer");
        } 
        #endregion

        #region 分配权限
        private void SetPerCommandFunc(User user)
        {
            string info = $"分配权限:{user.UserName}";
            OpenDialogForSetPer(user, "SetPer");
        }
        #endregion

        #region 权限对话框打开操作
        private void OpenDialogForSetPer(User user,string oprType)
        {
            DialogParameters dlgParams = new DialogParameters();
            dlgParams.Add("User", user);
            dlgParams.Add("OprType", oprType);
            _dialogService.ShowDialog("SetPermDialog", dlgParams, result =>
            {
                if (result.Result == ButtonResult.OK)
                {
                    var returnedUser = result.Parameters.GetValue<User>("returnedUser");
                    var index = UserList.IndexOf(UserList.Single(n => n.UserId == returnedUser.UserId));
                    UserList[index] = returnedUser; // 直接替换集合中的对象
                }
            });
        }
        #endregion

        private void InitUserData()
        {
            //UserList = new ObservableCollection<User>()
            //{
            //    new User(){UserId = 1,UserName = "罗纳尔多",PermDesc = "添加,删除,修改,查询",PersBinDesc = "1111"},
            //    new User(){UserId = 2,UserName = "里瓦尔多",PermDesc = "添加,删除,修改",PersBinDesc = "1110"},
            //    new User(){UserId = 3,UserName = "贝克哈姆",PermDesc = "添加,查询",PersBinDesc = "1001"}
            //};

            UserList = new ObservableCollection<User>()
            {
                new User(){UserId = 1,UserName = "罗纳尔多",PermDesc = "",PersBinDesc = "",PermVal=0},
                new User(){UserId = 2,UserName = "里瓦尔多",PermDesc = "",PersBinDesc = "",PermVal=0},
                new User(){UserId = 3,UserName = "贝克哈姆",PermDesc = "",PersBinDesc = "",PermVal=0}
            };

            foreach (var userItem in UserList)
            {
                userItem.RefreshData();
            }

        }
    }
}
